1 // Fetch a URL. Record the time. Return a result object.
2
3 import java.io.*;
4 import java.net.*;
5
6 public class Fetch {
7
8 static final int BUFFER_SIZE = 8192;
9
10 public Fetch() {
11
12 }
13
14 InputStream in = null;
15
16 public FetchResult get(URL url) {
17
18 int bytes = 1;
19 byte buf[] = new byte[BUFFER_SIZE];
20 FetchResult result = new FetchResult();
21
22 try {
23 result.start();
24
25 URLConnection inHTTP = url.openConnection();
26
27 inHTTP.setUseCaches (false);
28 in = inHTTP.getInputStream();
29
30 result.httpStatus = inHTTP.getHeaderField(0);
31
32 if (result.httpStatus == null) result.httpStatus = new String ("NO VID");
33
34 // Deplete the instream.
35 while (bytes > 0) {
36 bytes = in.read(buf);
37 result.size += bytes;
38 }
39
40 result.status = FetchResult.FR_OK;
41
42 } catch (ConnectException e) {
43 result.status = FetchResult.FR_CONNECT_FAILED;
44
45 } catch (IOException e) {
46 result.status = FetchResult.FR_READ_FAILED;
47
48 } catch (Exception e) {
49 result.status = FetchResult.FR_UNKNOWN_ERROR;
50
51 } finally {
52 try {
53 in.close();
54 } catch (Exception e) { }
55 }
56
57 result.end();
58 return result;
59
60 }
61
62 }
|